package com.lcw.library.imagepicker.data;

import android.net.Uri;
import android.os.Build;

/**
 * 媒体文件实体类
 * 
 * 用于存储图片和视频文件的基本信息，包括：
 * - 文件路径
 * - 文件名称
 * - 文件大小
 * - 媒体类型
 * - 视频时长
 * - 修改时间
 * 
 */
public class MediaFile {

    private String path;
    private Uri uri; // 添加Uri支持，适配分区存储
    private String mime;
    private Integer folderId;
    private String folderName;
    private long duration;
    private long dateModified;
    private long id; // 添加媒体ID，用于构建Uri
    private long size; // 添加文件大小信息

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public Uri getUri() {
        return uri;
    }

    public void setUri(Uri uri) {
        this.uri = uri;
    }

    /**
     * 获取用于访问文件的Uri，优先使用Uri（适配分区存储）
     * 在Android 10+建议使用Uri而不是直接文件路径
     *
     * @return
     */
    public Uri getAccessUri() {
        if (uri != null) {
            return uri;
        }
        // 如果没有Uri但有路径，尝试从路径构建Uri（向后兼容）
        if (path != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // 在分区存储环境下，应该优先使用MediaStore的Uri
            return uri;
        }
        return uri;
    }

    /**
     * 判断是否支持直接文件路径访问
     * Android 10+在分区存储下可能无法直接访问文件路径
     *
     * @return
     */
    public boolean canAccessByPath() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // Android 10+分区存储环境下，优先使用Uri
            return path != null && uri == null;
        }
        return path != null;
    }

    public String getMime() {
        return mime;
    }

    public void setMime(String mime) {
        this.mime = mime;
    }

    public Integer getFolderId() {
        return folderId;
    }

    public void setFolderId(Integer folderId) {
        this.folderId = folderId;
    }

    public String getFolderName() {
        return folderName;
    }

    public void setFolderName(String folderName) {
        this.folderName = folderName;
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public long getDateModified() {
        return dateModified;
    }

    public void setDateModified(long dateModified) {
        this.dateModified = dateModified;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    /**
     * 判断是否为图片文件
     *
     * @return
     */
    public boolean isImage() {
        return mime != null && mime.startsWith("image/");
    }

    /**
     * 判断是否为视频文件
     *
     * @return
     */
    public boolean isVideo() {
        return mime != null && mime.startsWith("video/");
    }

    /**
     * 判断是否为GIF文件
     *
     * @return
     */
    public boolean isGif() {
        return "image/gif".equals(mime);
    }

    @Override
    public String toString() {
        return "MediaFile{" +
                "path='" + path + '\'' +
                ", uri=" + uri +
                ", mime='" + mime + '\'' +
                ", folderId=" + folderId +
                ", folderName='" + folderName + '\'' +
                ", duration=" + duration +
                ", dateModified=" + dateModified +
                ", id=" + id +
                ", size=" + size +
                '}';
    }
}

